<?php
/**
 * Created by PhpStorm.
 * User: Lyq
 * Date: 2018/8/19
 * Time: 上午9:23
 */
namespace app\admin\controller;

use app\common\model\Settings;
use app\common\model\Message;

class System extends Common
{
    public function startImg(){
        if (request()->isGet())
        {
            //设置标题
            $this->templateTitle("启动图片");

            //设置内容
            $settings = Settings::getSettings(H_SETTINGS_START_IMG);
            $settings = empty($settings) ? ['img_url'=>'','link_url'=>''] : $settings;
            $this->assign("settings", $settings);
            $this->assign("single_image", array("img_url"=>$settings["img_url"]));

            return $this->fetch();
        }
        elseif (request()->isAjax())
        {

            $settings['img_url'] = input("img_url");
            $settings['link_url'] = input("link_url");

            Settings::setSettings(H_SETTINGS_START_IMG, $settings);

            $this->ajaxSuccess("设置成功！");
        }
    }
    //关于我们
    public function aboutUs()
    {
        if (request()->isGet())
        {
            //设置标题
            $this->templateTitle("关于我们");

            //设置内容
            $settings = Settings::getSettings(H_SETTINGS_ABOUT_US,2);
            $settings = empty($settings) ? "" : $settings;
            $this->assign("ueditor_content", ["content"=>$settings]);

            return $this->fetch();
        }
        elseif (request()->isAjax())
        {
            //帮助内容
            $content = input("content");

            Settings::setSettings(H_SETTINGS_ABOUT_US, $content,2);

            $this->ajaxSuccess("设置成功！");
        }
    }
    //联系我们
    public function contectUs()
    {
        if (request()->isGet())
        {
            //设置标题
            $this->templateTitle("联系我们");

            //设置内容
            $settings = Settings::getSettings(H_SETTINGS_CONTECT_US,2);
            $settings = empty($settings) ? "" : $settings;
            $this->assign("ueditor_content", ["content"=>$settings]);

            return $this->fetch();
        }
        elseif (request()->isAjax())
        {
            //帮助内容
            $content = input("content");

            Settings::setSettings(H_SETTINGS_CONTECT_US, $content,2);

            $this->ajaxSuccess("设置成功！");
        }
    }
    //邀请规则
    public function inviteRule()
    {
        if (request()->isGet())
        {
            //设置标题
            $this->templateTitle("邀请规则");

            //设置内容
            $settings = Settings::getSettings(H_SETTINGS_INVITE_RULE,2);
            $settings = empty($settings) ? "" : $settings;
            $this->assign("ueditor_content", ["content"=>$settings]);

            return $this->fetch();
        }
        elseif (request()->isAjax())
        {
            //内容
            $content = input("content");

            Settings::setSettings(H_SETTINGS_INVITE_RULE, $content,2);

            $this->ajaxSuccess("设置成功！");
        }
    }
    //服务协议
    public function treaty()
    {
        if (request()->isGet())
        {
            //设置标题
            $this->templateTitle("服务协议");

            //设置内容
            $settings = Settings::getSettings(H_SETTINGS_TREATY,2);
            $settings = empty($settings) ? "" : $settings;
            $this->assign("ueditor_content", ["content"=>$settings]);

            return $this->fetch();
        }
        elseif (request()->isAjax())
        {
            //协议内容
            $content = input("content");

            Settings::setSettings(H_SETTINGS_TREATY, $content,2);

            $this->ajaxSuccess("修改成功！");
        }
    }
    //全局配置
    public function setting()
    {
        if (request()->isGet())
        {
            //设置标题
            $this->templateTitle("系统配置");

            //设置内容
            $settings = Settings::getSettings(H_SETTINGS_GLOBAL,2);
            /*返回前台的数据*/
            $arr['system_title']=$settings['system_title'];
            $arr['server_tel']=@$settings['server_tel'];
            $this->assign('arr',$arr);
            /*图片处理*/
            $this->assign("single_image", array("logo"=>$settings['logo']));
            return $this->fetch();
        }
        elseif (request()->isAjax())
        {
            $data=input();
            //全局配置
            $arr = [
                'system_title'=>$data['system_title'],
                'server_tel'=>$data['server_tel'],
                'logo' => $data['logo'],
            ];
            Settings::setSettings(H_SETTINGS_GLOBAL,$arr,2);
            /*操作记录*/
            Message::admin_log(0,"后台修改全局配置！");
            $this->ajaxSuccess("配置成功！");
        }
    }

    /**
     * 支付设置
     */
    public function paySet()
    {
        if(request()->isGet()){
            $arr = Settings::getSettings('pay_set');
            $this->assign("arr", $arr);
            /*图片处理*/
            $this->assign("single_image", array("pay_code"=>$arr['pay_code']));
            return $this->fetch("paySet");
        }else{
            if(request()->isAjax()){
                $pay_code = input("pay_code");
                $bank_code_arr = input("bankCode/a");
                $bank_name_arr = input("bankName/a");
                $bank_user_arr = input("bankUser/a");
                $bank_list = [];
                foreach ($bank_code_arr as $key=>$value){
                    if(empty($value) || empty($bank_name_arr[$key]) || empty($bank_user_arr[$key])){
                        continue;
                    }
                    $item = [
                        "bank_code"=>$value,
                        "bank_name"=>$bank_name_arr[$key],
                        "bank_user"=>$bank_user_arr[$key]
                    ];
                    $bank_list[] = $item;
                }
                $data = [
                    "pay_code"=>$pay_code,
                    "bank_list"=>$bank_list
                ];

                Settings::setSettings("pay_set", $data);
                Message::admin_log(0, "修改支付设置");
                $this->ajaxSuccess("设置成功");
            }
        }
    }

    /**
     * 分销设置
     */
    public function commission()
    {
        if(request()->isGet()){
            $this->templateTitle('分销设置');
            $info = Settings::getSettings("commission");
            $this->assign("arr", $info);
            return $this->fetch('commission');
        }else{
            if(request()->isAjax()){
                $data = input();
                $arr = [
                    "commission1"=>$data["commission1"],
                    "commission2"=>$data["commission2"],
                    "commission3"=>$data["commission3"],
                ];
                Settings::setSettings("commission", $arr, 2);
                Message::admin_log(0, "后台修改分销设置");
                $this->ajaxSuccess("设置成功");
            }
        }
    }
}